package com.zzh.mybatis.first;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
 * shift+Alt+J
 */
import org.junit.Test;

import com.zzh.mybatis.po.User;

/**
 * @Title: MybatisFirst.java
 * @Package com.zzh.mybatis.first
 * @Description: TODO
 * @author zhangzhenhua
 * @date 2016年9月26日 下午12:07:07
 * @version V1.0
 */
public class MybatisFirst {

	// 根据id得到一条记录
	@Test
	public void findUserById() throws IOException {
		// mybatis配置文件
		String resource = "SqlMapConfig.xml";
		System.out.println(System.getProperty(resource));
		// 得到配置文件流
		InputStream inputStream = Resources.getResourceAsStream(resource);

		// 创建会话工厂
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

		// 通过工厂得到sqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();

		// 通过sqlSession 操作数据库
		// statement,映射文件中statement的Id,等于 = namespace+'.'+statement的id
		// parameter:制定映射文件中索赔配的parameterType类型的参数
		// 返回结果是映射文件中所匹配的resultType类型的对象
		User user = sqlSession.selectOne("test.findUserById", 1);
		System.out.println(user);
		sqlSession.close();

	}

	/* 根据用户名模糊查询用户 */
	@Test
	public void findUserByName() throws IOException {
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		List<User> list = sqlSession.selectList("test.finduserByName", "小明");
		System.out.println(list);
	}

	/* 添加用户信息 */
	@Test
	public void insertUser() throws IOException{ 
		/* 加载配置文件 */
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		/* 创建会话工厂 */
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
		/*通过sqlsession操作数据库*/
		SqlSession sqlSession = sessionFactory.openSession();
		User user = new User();
		user.setUsername("小华2");
		user.setBirthday(new Date());
		user.setSex("1");
		user.setAddress("上海");
		
		sqlSession.insert("test.insertUser", user);
		
		/* 提交事务 -不提交会回滚*/
		sqlSession.commit();
		
		//System.out.println(user.getId());
		/* 关闭SqlSession */
		sqlSession.close();
		
		
	}
	
	
	/* 删除用户 */
	@Test
	public void deleteUser() throws IOException{
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
		SqlSession ss = ssf.openSession();
		ss.delete("test.deleteUser", 39);
		ss.commit();
		ss.close();
		
	}
	
	/* 更新用户资料 */
	
	@Test
	public void updateUser() throws IOException{
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
		SqlSession ss = ssf.openSession();
		User user = new User();
		user.setId(39);
		user.setUsername("小王");
		user.setBirthday(new Date());
		user.setSex("2");
		user.setAddress("北京");
		
		ss.update("test.updateUser", user);
		
		ss.commit();
		ss.close();
		
		
		
		
		
		
	}
	
	
	/* 测试 */ 
	@Test
	public void testMethord(){
		System.out.println(new Date());
		
		
	}
	
	
	
	
	
	
	
	

}
